home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 November: Tool Chest / Dev.CD Nov 94.toast / Sample Code / Snippets / Development Tools & Languages / MacApp C++ Load⁄dump / DemoTextDump 2 / UDemoText.cp < prev    next >
Encoding:
Text File  |  1992-07-15  |  27.7 KB  |  875 lines  |  [TEXT/MPS ]

  1. // Copyright © 1989-1990 by Apple Computer, Inc.  All rights reserved. 
  2.  
  3.  
  4. #ifndef __DemoTextDump__
  5. #include "DemoTextDump.h"
  6. #endif
  7.  
  8.  
  9. #ifndef qDumpOwn
  10. #include "UDemoText.h"
  11. #endif QDumpOwn
  12.  
  13.  
  14. extern "C" char* memcpy(void*, void*, int);
  15. #define pstrcpy(p,q) memcpy(p, q, q[0]+1)                    // added for translation: Pascal strcpy
  16. extern "C" void printf(const char *, ...);
  17.  
  18.     // Menu numbers 
  19. const short mFont                        = 10;
  20.  
  21.     // Command numbers 
  22.  
  23. const short cWidthFrame                 = 601;                // View-width-determination commands
  24. const short cWidthView                    = 602;
  25. const short cWidthOnePage                = 603;
  26.  
  27. const short cHeightFrame                = 604;                // View-height-determination commands
  28. const short cHeightPages                = 605;
  29. const short cHeightText                 = 606;
  30. const short cHeightConst                = 607;
  31.  
  32. const short cJustLeft                    = 608;                // Justification commands
  33. const short cJustCenter                 = 609;
  34. const short cJustRight                    = 610;
  35.  
  36.   /*
  37.  Command numbering scheme:
  38. const short styles = 1000
  39. const short sizes = 1100
  40. const short just = 1200
  41. const short fonts = 1300
  42. const short hiers = 1400
  43. const short colors = 1500
  44.   */
  45.  
  46.     // Command numbers for typestyle attributes
  47. const short cPlainText                    = 1001;
  48. const short cBold                        = 1002;
  49. const short cItalic                     = 1003;
  50. const short cUnderline                    = 1004;
  51. const short cOutline                    = 1005;
  52. const short cShadow                     = 1006;
  53. const short cCondense                    = 1007;
  54. const short cExtend                     = 1008;
  55.  
  56.     // Command numbers for font-size commands
  57. const short cSizeChange                 = 1100;
  58. const short cSizeBase                    = 1100;
  59. const short cSizeMin                    = 1109;
  60. const short cSizeMax                    = 1124;
  61.     // 1101-1197 reserved for font sizes 1-97 pts.
  62. const short cSizeGrow                    = 1198;
  63. const short cSizeShrink                 = 1199;
  64.  
  65.     // Command numbers to cover other stylistic changes
  66. const short cJustChange                 = 1200;
  67. const short cFontChange                 = 1300;
  68.  
  69.     // Command numbers for the hierarchial menu
  70. const short cStyle                        = 1401;
  71. const short cSize                        = 1402;
  72. const short cFont                        = 1403;
  73. const short cColor                        = 1404;
  74.  
  75.     // Command numbers for changing colors
  76. const short cColorChange                = 1500;
  77. const short cColorText                    = 1501;
  78. const short cColorBackground            = 1502;
  79.  
  80. const short cXShowBreaks                = 1601;
  81.  
  82.     // Constant for staggering windows
  83. const short kStaggerAmount                = 16;
  84.  
  85.     // Constant for amount to relative size text selection
  86. const short kRelSizeAmount                = 4;
  87.  
  88.     // Constants for the text specs resource
  89. const unsigned long kTextSpecsRsrcType    = 'SPEC';
  90. const short kTextSpecsRsrcID            = 1;
  91.  
  92.     // Constants for the text style resource
  93. const unsigned long kTextStyleRsrcType    = 'STYL';
  94. const short kStylesRsrcID                = 1;
  95. const short kElementsRsrcID             = 2;
  96.  
  97.     // Constants for the print info resource
  98. const unsigned long kPrintInfoRsrcType    = 'PRNT';
  99. const short kPrintInfoRsrcID            = 1;
  100.  
  101.     // Constants for the prompts string list
  102. const short kPromptsRsrcID                = 1001;
  103. const short kColTextPrompt                = 1;
  104. const short kColBackPrompt                = 2;
  105.  
  106.     // The 'File is too large' alert
  107. const short kFileTooBig                 = 1000;
  108.  
  109. const short kHierDisplayedMBar            = 131;            // Displayed menus on hier. menu system
  110. const short kNonHierDisplayedMBar         = 128;            // Displayed menus on non-hier. system
  111.  
  112. const short kHierMenuOffset             = 1000;         // Offset added to non-hier menu cmds to get
  113.                                                         //  right #
  114.  
  115. TextSpecs    gDefaultSpecs;                                // Text specs as default for new documents
  116. short        gStaggerCount;
  117. short        gMenuOfs;
  118. Str255        gPromptString;
  119.  
  120. //--------------------------------------------------------------------------------------------------
  121. #pragma segment ARes
  122.  
  123. StringPtr GetPrompt(short index)
  124. {
  125.     GetIndString(gPromptString, kPromptsRsrcID, index);
  126.     return (StringPtr) &gPromptString;
  127. }
  128.  
  129. //******************************************************************************************
  130. //  T D e m o T e x t A p p l i c a t i o n
  131. //******************************************************************************************
  132. //--------------------------------------------------------------------------------------------------
  133. #pragma segment AInit
  134.  
  135. pascal void TDemoTextApplication::IDemoTextApplication()
  136. {
  137.     
  138.     Str255    fontName;
  139.     TTEView    *aTEView;
  140.  
  141. #if qNeedsHierarchialMenus == 0
  142.     if (! gConfiguration.hasHierarchicalMenus) {
  143.         gMBarDisplayed = kNonHierDisplayedMBar;
  144.         gMenuOfs = 0;
  145.     } else
  146. #endif
  147.     {
  148.         gMBarDisplayed = kHierDisplayedMBar;
  149.         gMenuOfs = kHierMenuOffset;
  150.     }
  151.  
  152.     IApplication(kFileType);
  153.  
  154.     if (! gFinderPrinting) {
  155.         AddResMenu(GetMHandle(mFont), 'FONT');
  156.  
  157.         gStaggerCount = 0;
  158.  
  159.         SetStyle(cBold, bold);
  160.         SetStyle(cUnderline, underline);
  161.         SetStyle(cItalic, italic);
  162.         SetStyle(cOutline, outline);
  163.         SetStyle(cShadow, shadow);
  164.         SetStyle(cCondense, condense);
  165.         SetStyle(cExtend, extend);
  166.     }
  167.  
  168.     // Instead of hard-wiring a default text style, get it from the TEView resource by
  169.     aTEView = (TTEView*) DoCreateViews(NULL, NULL, kViewRsrcID, &gZeroVPt);
  170.     FailNIL(aTEView);
  171.     GetFontName(aTEView->fTextStyle.tsFont, fontName);
  172.     //gDefaultSpecs.theTextFont = fontName;                        // Set up initial text specs
  173.     pstrcpy(gDefaultSpecs.theTextFont, fontName);                // Kludged Pascal string copy
  174.     gDefaultSpecs.theTextFace = aTEView->fTextStyle.tsFace;
  175.     gDefaultSpecs.theTextSize = aTEView->fTextStyle.tsSize;
  176.     gDefaultSpecs.theTextColor = aTEView->fTextStyle.tsColor;
  177.     gDefaultSpecs.theJustification = aTEView->fJustification;
  178.     gDefaultSpecs.theBackColor = gRGBWhite;
  179.     aTEView->Free();
  180. }
  181.  
  182. //--------------------------------------------------------------------------------------------------
  183. #pragma segment AOpen
  184.  
  185. pascal TDocument *TDemoTextApplication::DoMakeDocument(CmdNumber )
  186. {
  187.     TTextDocument    *aTextDocument;
  188.  
  189.     aTextDocument = new TTextDocument;
  190.     FailNIL(aTextDocument);
  191.     aTextDocument->ITextDocument();
  192.     return aTextDocument;
  193. }
  194.  
  195. //--------------------------------------------------------------------------------------------------
  196. #if qDebug
  197. #pragma segment ADebug
  198.  
  199. pascal void TDemoTextApplication::IdentifySoftware()
  200. {
  201.     printf("DemoText Source date: 23 April 86; Compiled: %s @ %s\n", __DATE__, __TIME__);
  202.     inherited::IdentifySoftware();
  203. }
  204. #endif
  205.  
  206. //******************************************************************************************
  207. //  T T e x t D o c u m e n t
  208. //******************************************************************************************
  209. //--------------------------------------------------------------------------------------------------
  210. #pragma segment AOpen
  211.  
  212. pascal void TTextDocument::ITextDocument()
  213. {
  214.     fDocText = NULL;
  215.     IDocument(kFileType, kSignature, kUsesDataFork, kUsesRsrcFork, ! kDataOpen, ! kRsrcOpen);
  216.  
  217.     fTEView = NULL;
  218.     fStyles = NULL;
  219.     fElements = NULL;
  220.     fDocText = NewPermHandle(0);
  221.     FailNIL(fDocText);
  222. }
  223.  
  224. //--------------------------------------------------------------------------------------------------
  225. #pragma segment AClose
  226.  
  227. pascal void TTextDocument::Free()
  228. {
  229.     if (fDocText)
  230.         DisposHandle(fDocText);
  231.     inherited::Free();
  232. }
  233.  
  234. //--------------------------------------------------------------------------------------------------
  235. #pragma segment ANonRes
  236.  
  237. pascal void TTextDocument::ChangeBackColor(RGBColor *newColor)
  238. {
  239.     GrafPtr    oldPort;
  240.     TWindow    *itsWindow;
  241.  
  242.  // ??? Is this right?  Should we set the background color even if the view isn't
  243.  //  in a window yet?
  244.     GetPort(&oldPort);
  245.     SetPort((*fTEView->fHTE)->inPort);
  246.     RGBBackColor(newColor);
  247.     itsWindow = fTEView->GetWindow();
  248.     if (itsWindow)
  249.         itsWindow->ForceRedraw();
  250.     SetPort(oldPort);
  251. }
  252.  
  253. //--------------------------------------------------------------------------------------------------
  254. #pragma segment AOpen
  255.  
  256. pascal void TTextDocument::DoInitialState()
  257. {
  258.     fTextSpecs = gDefaultSpecs;
  259. }
  260.  
  261. //--------------------------------------------------------------------------------------------------
  262. #pragma segment AOpen
  263.  
  264. pascal void TTextDocument::DoMakeViews(Boolean forPrinting)
  265. {
  266.     TView                *aView;
  267.     TStdPrintHandler    *aHandler;
  268.  
  269.     if (forPrinting)
  270.     // We're only finder printing--don't need a window, just the view being printed 
  271.         aView = DoCreateViews(this, NULL, kViewRsrcID, &gZeroVPt);
  272.     else
  273.         aView = NewTemplateWindow(kWindowRsrcID, (TDocument*) this);
  274.  
  275.     FailNIL(aView);                                     // ??? Will we have already failed?
  276.     aView = aView->FindSubView('TEVW');
  277.     fTEView = (TTEView*) aView;                            // Must cast because FindSubView returns
  278.                                                         /// TView 
  279.     aHandler = new TStdPrintHandler;
  280.     FailNIL(aHandler);
  281.     aHandler->IStdPrintHandler((TDocument*) this,         // its document
  282.                               (TView*) fTEView,            // its view
  283.                               0,                        // does not have square dots
  284.                               1,                         // horzontal page size is fixed
  285.                               0);                        // vertical page size is variable (could be
  286.                                                         //  set
  287.     // …to true on non-style TE systems)
  288.     aHandler->fMinimalMargins = 0;
  289.     ShowReverted();
  290. }
  291.  
  292. //--------------------------------------------------------------------------------------------------
  293. #pragma segment ASelCommand
  294.  
  295. static SizeDeterminer    sd;                // for shared use by the following functions
  296. static Str255            aName;            // which are used by TTextDocument::DoMenuCommand()
  297. static short            menu;
  298. static short            item;
  299. static TextStyle        newStyle;
  300. static CmdNumber        aCmdNumber;
  301. static TTextDocument    *ttd;            // kludged copy of "this"
  302.  
  303. //--------------------------------------------------------------------------------------------------
  304.  
  305. pascal static TCommand *DoSizeChange(CmdNumber base)
  306. {
  307.     newStyle.tsSize = aCmdNumber - base;
  308.     ttd->fTextSpecs.theTextSize = newStyle.tsSize;
  309.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cSizeChange, doSize + doToggle);
  310. }
  311.  
  312. //--------------------------------------------------------------------------------------------------
  313.  
  314. pascal static TCommand *DoRelSizeChange(short amount)
  315. {
  316.     newStyle.tsSize = amount;
  317.     ttd->fTextSpecs.theTextSize += amount;
  318.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cSizeChange, doSize + addSize + doToggle);
  319. }
  320.  
  321. //--------------------------------------------------------------------------------------------------
  322.  
  323. pascal static TCommand *DoFontChange()
  324. {
  325.     GetItem(GetMHandle(menu), item, aName);
  326.     GetFNum(aName, &newStyle.tsFont);
  327.     //ttd->fTextSpecs.theTextFont = aName;
  328.     pstrcpy(ttd->fTextSpecs.theTextFont, aName);        // kludged Pascal string copy
  329.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cFontChange, doFont + doToggle);
  330. }
  331.  
  332. //--------------------------------------------------------------------------------------------------
  333.  
  334. pascal static TCommand *DoColTextChange()
  335. {
  336.     RGBColor    aColor;
  337.     static Point P4040 = {0x0040, 0x0040};
  338.  
  339.     aColor = ttd->fTextSpecs.theTextColor;
  340.     if (GetColor(P4040, GetPrompt(kColTextPrompt), &aColor, &newStyle.tsColor)) {
  341.         ttd->fTextSpecs.theTextColor = newStyle.tsColor;
  342.         return ttd->fTEView->DoMakeStyleCommand(&newStyle, cColorChange, doColor + doToggle);
  343.     }
  344.     return gNoChanges;
  345. }
  346.  
  347. //--------------------------------------------------------------------------------------------------
  348.  
  349. pascal static void DoColBackChange()
  350. {
  351.     RGBColor    aColor;
  352.     static Point P4040 = {0x0040, 0x0040};
  353.  
  354.     aColor = ttd->fTextSpecs.theBackColor;
  355.     if (GetColor(P4040, GetPrompt(kColBackPrompt), &aColor, &newStyle.tsColor)) {
  356.         ttd->fTextSpecs.theBackColor = newStyle.tsColor;
  357.         ttd->ChangeBackColor(&newStyle.tsColor);
  358.     }
  359. }
  360.  
  361. //--------------------------------------------------------------------------------------------------
  362.  
  363. pascal static TCommand *DoJustChange()
  364. {
  365.     short            newJust;
  366.     TJustCommand    *aJustChange;
  367.  
  368.     switch (aCmdNumber) {
  369.         case cJustLeft:        newJust = teJustLeft;    break;
  370.         case cJustCenter:    newJust = teJustCenter;    break;
  371.         case cJustRight:    newJust = teJustRight;    break;
  372.     }
  373.     aJustChange = new TJustCommand;
  374.     FailNIL(aJustChange);
  375.     aJustChange->IJustCommand(ttd->fTEView, newJust);
  376.     ttd->fTextSpecs.theJustification = newJust;
  377.     return aJustChange;
  378. }
  379.  
  380. //--------------------------------------------------------------------------------------------------
  381.  
  382. pascal static TCommand *DoPlainChange()
  383. {
  384.     newStyle.tsFace = 0;
  385.     ttd->fTextSpecs.theTextFace = 0;
  386.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cStyleChange, doFace);
  387. }
  388.  
  389. //--------------------------------------------------------------------------------------------------
  390.  
  391. pascal static TCommand *DoStyleChange()
  392. {
  393.     switch (aCmdNumber) {
  394.         case cBold:            newStyle.tsFace = bold;            break;
  395.         case cItalic:        newStyle.tsFace = italic;        break;
  396.         case cUnderline:    newStyle.tsFace = underline;    break;
  397.         case cOutline:        newStyle.tsFace = outline;        break;
  398.         case cShadow:        newStyle.tsFace = shadow;        break;
  399.         case cCondense:        newStyle.tsFace = condense;        break;
  400.         case cExtend:        newStyle.tsFace = extend;        break;
  401.     }
  402.     if (newStyle.tsFace * ttd->fTextSpecs.theTextFace == newStyle.tsFace)
  403.         ttd->fTextSpecs.theTextFace -= newStyle.tsFace;
  404.     else
  405.         ttd->fTextSpecs.theTextFace += newStyle.tsFace;
  406.     return ttd->fTEView->DoMakeStyleCommand(&newStyle, cStyleChange, doFace + doToggle);
  407. }
  408.  
  409. //--------------------------------------------------------------------------------------------------
  410.  
  411. pascal static void InstallChangedDeterminer(VHSelect vhs)
  412. {
  413.     if (sd != ttd->fTEView->fSizeDeterminer[vhs]) {
  414.         ttd->fTEView->fSizeDeterminer[vhs] = sd;
  415.  
  416. // If we changed the horizontal size determiner, we must
  417. // ask the TTEView to recompute the TE rectangles.
  418.         if (vhs == h) {
  419.             if (sd == sizeSuperView)
  420.                 ttd->fTEView->SuperViewChangedSize(&gZeroVPt, 1); // Don't care about deltas
  421.             else if (sd == sizePage)
  422.                 ttd->fTEView->DoPagination();
  423.         }
  424.  
  425.         ttd->fTEView->AdjustSize();
  426.         ttd->fTEView->ForceRedraw();
  427.     }
  428. }
  429.  
  430. //--------------------------------------------------------------------------------------------------
  431.  
  432. pascal static void DoWidthChange()
  433. {
  434.     if (aCmdNumber == cWidthFrame)
  435.         sd = sizeSuperView;
  436.     else if (aCmdNumber == cWidthOnePage)
  437.         sd = sizePage;
  438.     else
  439.         sd = sizeFixed;
  440.  
  441.     InstallChangedDeterminer(h);                    // NB: The following is not undoable in the
  442.                                                     // current version
  443. }
  444.  
  445. //--------------------------------------------------------------------------------------------------
  446.  
  447. pascal static void DoHeightChange()
  448. {
  449.     switch (aCmdNumber) {
  450.         case cHeightFrame:    sd = sizeSuperView;    break;
  451.         case cHeightPages:    sd = sizeFillPages;    break;
  452.         case cHeightText:    sd = sizeVariable;    break;
  453.         case cHeightConst:    sd = sizeFixed;        break;
  454.     }
  455.  
  456.     InstallChangedDeterminer(v);                    // NB: The following is not undoable in the
  457.                                                     // current version
  458. }
  459.  
  460. pascal TCommand *TTextDocument::DoMenuCommand(CmdNumber theCmdNumber)
  461. {
  462.     aCmdNumber = theCmdNumber;        // for use in formerly nested procedures
  463.     ttd = this;                        // ditto
  464.  
  465.     CmdToMenuItem(aCmdNumber, &menu, &item);
  466.  
  467.     if (menu == mFont) {
  468.         return DoFontChange();
  469.     } else {
  470.         if (aCmdNumber >= cSizeMin && aCmdNumber <= cSizeMax)
  471.             return DoSizeChange(cSizeBase);
  472.         else if (aCmdNumber == cSizeGrow)
  473.             return DoRelSizeChange(kRelSizeAmount);
  474.         else if (aCmdNumber == cSizeShrink)
  475.             return DoRelSizeChange( - kRelSizeAmount);
  476.         else if (aCmdNumber >= cJustLeft && aCmdNumber <= cJustRight)
  477.             return DoJustChange();
  478.         else if (aCmdNumber == cPlainText)
  479.             return DoPlainChange();
  480.         else if (aCmdNumber >= cBold && aCmdNumber <= cExtend)
  481.             return DoStyleChange();
  482.         else if (aCmdNumber == cColorText)
  483.             return DoColTextChange();
  484.         else if (aCmdNumber == cColorBackground)
  485.             DoColBackChange();
  486.         else if (aCmdNumber >= cWidthFrame && aCmdNumber <= cWidthOnePage)
  487.             DoWidthChange();
  488.         else if (aCmdNumber >= cHeightFrame && aCmdNumber <= cHeightConst)
  489.             DoHeightChange();
  490.             // ??? Next line has got to change ??????????
  491.         else if (aCmdNumber == cXShowBreaks)
  492.             return fDocPrintHandler->DoMenuCommand(cShowBreaks);
  493.         else
  494.             return inherited::DoMenuCommand(aCmdNumber);
  495.     }
  496.     return gNoChanges;
  497. }
  498.  
  499. //--------------------------------------------------------------------------------------------------
  500. #pragma segment AWriteFile
  501.  
  502. pascal void TTextDocument::DoNeedDiskSpace(long  *dataForkBytes, long *rsrcForkBytes)
  503. {
  504.     TEStyleHandle    styles;
  505.     STHandle        elements;
  506.  
  507.     *dataForkBytes += GetHandleSize(fDocText);
  508.  
  509.     *rsrcForkBytes += kPrintInfoSize + kRsrcTypeOverhead + kRsrcOverhead;
  510.  
  511.     if (fTEView->fStyleType == kWithStyle && fTEView->fHTE) {
  512.         fTEView->ExtractStyles(&styles, &elements);
  513.         *rsrcForkBytes += GetHandleSize(Handle(styles)) + kRsrcTypeOverhead +
  514.                          kRsrcOverhead + GetHandleSize(Handle(elements)) + kRsrcTypeOverhead +
  515.                          kRsrcOverhead;
  516.     }
  517.  
  518.     *rsrcForkBytes += sizeof(TextSpecs) + kRsrcTypeOverhead + kRsrcOverhead;
  519.  
  520.     // Get resource file overhead 
  521.     inherited::DoNeedDiskSpace(dataForkBytes, rsrcForkBytes);
  522. }
  523.  
  524. //--------------------------------------------------------------------------------------------------
  525. #pragma segment AReadFile
  526.  
  527. pascal void TTextDocument::DoRead(short aRefNum, Boolean , Boolean )
  528. {
  529.     long            numChars;
  530.     TextSpecsHdl    hTextSpecs;
  531.     Handle            hPrintInfo;
  532.  
  533.     FailOSErr(GetEOF(aRefNum, &numChars));            // Read in the text 
  534.  
  535.     if (numChars > kUnlimited) {                            // The file may have been created by…
  536.         gApplication->ShowError(0, msgAlert + kFileTooBig);    // …someone else - limit it to 32K!
  537.         numChars = kUnlimited;
  538.     }
  539.  
  540.     SetHandleSize(fDocText, numChars);
  541.     FailMemError();
  542.     FailOSErr(FSRead(aRefNum, &numChars, *fDocText));
  543.  
  544.     fStyles = TEStyleHandle(GetResource(kTextStyleRsrcType, kStylesRsrcID)); // Read in the TEStyleRec
  545.     if (fStyles)
  546.         DetachResource(Handle(fStyles));
  547.  
  548.     fElements =                                            // Read in the STElement array
  549.       STHandle(GetResource(kTextStyleRsrcType, kElementsRsrcID));
  550.     if (fElements)
  551.         DetachResource(Handle(fElements));
  552.  
  553.     hTextSpecs =                                        // Read the text specs resource
  554.       TextSpecsHdl(GetResource(kTextSpecsRsrcType, kTextSpecsRsrcID));
  555.     if (hTextSpecs)
  556.         fTextSpecs = **hTextSpecs;
  557.     else
  558.         DoInitialState();
  559.  
  560.     hPrintInfo =                                        // Read the print info resource
  561.       GetResource(kPrintInfoRsrcType, kPrintInfoRsrcID);
  562.     if (hPrintInfo) {                                    // no print info resources was saved
  563.         if (fPrintInfo == NULL) {
  564.             fPrintInfo = NewPermHandle(kPrintInfoSize);
  565.             FailNIL(fPrintInfo);
  566.         }
  567.         BlockMove(*hPrintInfo, *fPrintInfo, kPrintInfoSize);
  568.     }
  569. }
  570.  
  571. //--------------------------------------------------------------------------------------------------
  572. #pragma segment ARes
  573.  
  574. pascal void TTextDocument::DoSetupMenus()
  575. {
  576.     Boolean            hasColor;
  577.     Boolean            hasStyle;
  578.     Boolean            checkPlain;
  579.     Boolean            checkSize;
  580.     Boolean            checkFont;
  581.     Boolean            specChange;
  582.     short            just;
  583.     short            item;
  584.     short            fnt;
  585.     short            c;
  586.     short            aMode;
  587.     Style            aFace;
  588.     SizeDeterminer    sd;
  589.     MenuHandle        aMenuHandle;
  590.     Str255            aName;
  591.     TextStyle        aStyle;
  592.     short            theFont;
  593.     short            startOfSelection, endOfSelection;
  594.     Str255            aStr255;
  595.  
  596.     inherited::DoSetupMenus();
  597.  
  598.     hasColor = gConfiguration.hasColorQD;
  599.     hasStyle = gConfiguration.hasStyleTextEdit;
  600.  
  601. #if qNeedsStyleTextEdit == 0
  602.     if (! hasStyle) {
  603.         //aStr255 = fTextSpecs.theTextFont;
  604.         pstrcpy(aStr255, fTextSpecs.theTextFont);        // kludged Pascal string copy
  605.         GetFNum(aStr255, &aStyle.tsFont);
  606.         aStyle.tsFace = fTextSpecs.theTextFace;
  607.         aStyle.tsSize = fTextSpecs.theTextSize;
  608.         aStyle.tsColor = fTextSpecs.theTextColor;
  609.         checkPlain = aStyle.tsFace = 0;
  610.         checkFont = 1;
  611.     } else
  612. #endif
  613.     {
  614.         startOfSelection = (*fTEView->fHTE)->selStart;
  615.         endOfSelection = (*fTEView->fHTE)->selEnd;
  616.         aMode = doFace;
  617.         checkPlain = fTEView->ContinuousStyle(startOfSelection, endOfSelection, &aMode, &aStyle) &&
  618.                       (aStyle.tsFace == 0);
  619.  
  620.         aMode = doAll;
  621.         aStyle.tsFace = bold | italic | underline | outline | shadow | extend | condense;
  622.         specChange = fTEView->ContinuousStyle(startOfSelection, endOfSelection, &aMode, &aStyle);
  623.         checkFont = ((aMode & doFont) != 0);
  624.     }
  625.  
  626.     aMenuHandle = GetMHandle(mFont);
  627.     // If specs have changed AND EITHER an old-style record OR the font's continuous - Ed.
  628.     specChange = fTEView->fSpecsChanged && ((! hasStyle) || checkFont);
  629.  
  630.     GetFontName(aStyle.tsFont, aName);                    // Get real font number in case tsFont is
  631.     GetFNum(aName, &theFont);                            // …the system or application font.
  632.     for (item=1; item <= CountMItems(aMenuHandle); item++) {
  633.   // There can be more than 31 menu entries with scrolling menus, but trying to enable
  634.   // an item with number > 31 is bad news.  If the menu itself is enabled (which it
  635.   // will be in MacApp if any of the first 31 items is enabled), then the extras
  636.   // will always be enabled.
  637.         if (item <= 31)
  638.             EnableItem(aMenuHandle, item);
  639.         if (checkFont) {
  640.             GetItem(aMenuHandle, item, aName);
  641.             GetFNum(aName, &fnt);
  642.             CheckItem(aMenuHandle, item, fnt == theFont);
  643.         }
  644.     }
  645.  
  646.     EnableCheck(cXShowBreaks, 1, ((TStdPrintHandler*)fDocPrintHandler)->fShowBreaks);
  647.  
  648.     sd = fTEView->fSizeDeterminer[h];                    // Enable size determiner related menu items
  649.  
  650.     EnableCheck(cWidthFrame, 1, (sd == sizeSuperView));
  651.     EnableCheck(cWidthOnePage, 1, (sd == sizePage));
  652.     EnableCheck(cWidthView, 1, (sd == sizeFixed));
  653.  
  654.     sd = fTEView->fSizeDeterminer[v];
  655.     EnableCheck(cHeightFrame, 1, (sd == sizeSuperView));
  656.     EnableCheck(cHeightPages, 1, (sd == sizeFillPages));
  657.     EnableCheck(cHeightText, 1, (sd == sizeVariable));
  658.     EnableCheck(cHeightConst, 1, (sd == sizeFixed));
  659.  
  660.     just = fTEView->fJustification;                     // Enable justification related menu items
  661.     EnableCheck(cJustLeft, 1, (just == teJustLeft));
  662.     EnableCheck(cJustCenter, 1, (just == teJustCenter));
  663.     EnableCheck(cJustRight, 1, (just == teJustRight));
  664.  
  665. #if qNeedsHierarchialMenus == 0
  666.     if (gConfiguration.hasHierarchicalMenus)
  667. #endif
  668.     {
  669.         Enable(cStyle, 1);                                //  Enable sub-menus
  670.         Enable(cSize, 1);
  671.         Enable(cFont, 1);
  672.         Enable(cColor, hasColor);
  673.     }
  674.  
  675.     aFace = aStyle.tsFace;
  676.     
  677.     EnableCheck(cPlainText, 1, checkPlain);                // Enable normal Style menu items
  678.     EnableCheck(cBold, 1,      aFace & bold);
  679.     EnableCheck(cItalic, 1,    aFace & italic);
  680.     EnableCheck(cUnderline, 1, aFace & underline);
  681.     EnableCheck(cOutline, 1,   aFace & outline);
  682.     EnableCheck(cShadow, 1,    aFace & shadow);
  683.     EnableCheck(cCondense, 1,  aFace & condense);
  684.     EnableCheck(cExtend, 1,    aFace & extend);
  685.  
  686.     for (c=cSizeMin; c<=cSizeMax; c++) {
  687.         if (hasStyle && ((aMode & doSize) == 0))
  688.             checkSize = 0;
  689.         else
  690.             checkSize = (c - cSizeBase) == aStyle.tsSize;
  691.         EnableCheck(c, 1, checkSize);
  692.         if (fTEView->fSpecsChanged) {
  693.             if ( ((! hasStyle) ||                        // If the record isn't styled, or
  694.                  (aMode & doFont)) &&                    // …it _is_ styled and font is continuous
  695.                RealFont(aStyle.tsFont,c-cSizeBase) )    // …and the size is a real one
  696.                 aFace = outline;                        // …then we outline it
  697.             else
  698.                 aFace = 0;
  699.             SetStyle(c, aFace);
  700.         }
  701.     }
  702.  
  703.     Enable(cSizeGrow, 1);
  704.     Enable(cSizeShrink, 1);
  705.  
  706.     Enable(cColorText, hasColor);
  707.     Enable(cColorBackground, hasColor);
  708.  
  709.     fTEView->fSpecsChanged = 0;
  710. }
  711.  
  712. //--------------------------------------------------------------------------------------------------
  713. #pragma segment AWriteFile
  714.  
  715. pascal void TTextDocument::DoWrite(short aRefNum, Boolean )
  716. {
  717.     long            numChars;
  718.     TextSpecsHdl    hTextSpecs;
  719.     Handle            tempHandle;
  720.     TEStyleHandle    styles;
  721.     STHandle        elements;
  722.  
  723.     // Write out the text
  724.     numChars = GetHandleSize(fDocText);
  725.     FailOSErr(FSWrite(aRefNum, &numChars, *fDocText));
  726.  
  727.     if (fTEView->fStyleType == kWithStyle) {
  728.         fTEView->ExtractStyles(&styles, &elements);
  729.         FailOSErr(HandToHand((Handle*)&styles));
  730.         AddResource(Handle(styles), kTextStyleRsrcType, kStylesRsrcID, "\p");
  731.         FailResError();
  732.         FailOSErr(HandToHand((Handle*)&elements));
  733.         AddResource(Handle(elements), kTextStyleRsrcType, kElementsRsrcID, "\p");
  734.         FailResError();
  735.     }
  736.  
  737.     // Write the text specification resource, after converting it to a handle
  738.     hTextSpecs = TextSpecsHdl(NewHandle(sizeof(TextSpecs)));
  739.     FailNIL(hTextSpecs);
  740.     **hTextSpecs = fTextSpecs;
  741.     AddResource(Handle(hTextSpecs), kTextSpecsRsrcType, kTextSpecsRsrcID, "\p");
  742.  
  743.     FailResError();
  744.  
  745.  // Write the print info resource.  Note we can't use MacApp for this because MacApp
  746.  // will write the print info into the data fork.  Note also--we must copy the print
  747.  // info resource to another handle because the Resource Manager will dispose of the
  748.  // resource handles when a resource fork is closed.
  749.     if (fPrintInfo) {                            // Make sure we have one of these guy
  750.         tempHandle = fPrintInfo;
  751.         FailOSErr(HandToHand(&tempHandle));
  752.         AddResource(tempHandle, kPrintInfoRsrcType, kPrintInfoRsrcID, "\p");
  753.         FailResError();
  754.     }
  755. }
  756.  
  757. //--------------------------------------------------------------------------------------------------
  758. #pragma segment AClose
  759.  
  760. pascal void TTextDocument::FreeData()
  761. {
  762.     SetHandleSize(fDocText, 0);
  763. }
  764.  
  765. //--------------------------------------------------------------------------------------------------
  766. #pragma segment ARes
  767.  
  768. pascal void TTextDocument::SetSpecStyle()
  769. {
  770.     short        theFont;
  771.     TextStyle    theTS;
  772.     Str255        aStr255;
  773.  
  774.     //aStr255 = fTextSpecs.theTextFont;
  775.     pstrcpy(aStr255, fTextSpecs.theTextFont);    // kludged Pascal string copy
  776.     GetFNum(aStr255, &theFont);
  777.     SetTextStyle(&theTS, theFont, fTextSpecs.theTextFace,
  778.                      fTextSpecs.theTextSize, &fTextSpecs.theTextColor);
  779.     fTEView->SetOneStyle(0, 0, doAll, &theTS, kDontRedraw);
  780. }
  781.  
  782. //--------------------------------------------------------------------------------------------------
  783. #pragma segment AReadFile
  784.  
  785. pascal void TTextDocument::ShowReverted()
  786. {
  787.     RGBColor    aColor;
  788.  
  789.     fTEView->StuffText(fDocText);                        // put in the text
  790.     TESetSelect(0, 0, fTEView->fHTE);
  791.     if (fStyles && fElements)                            // If we're able, stuff style info
  792.         fTEView->StuffStyles(fStyles, fElements);
  793.     else
  794.         SetSpecStyle();
  795. #if qNeedsColorQD == 0
  796.     if (gConfiguration.hasColorQD)
  797. #endif
  798.     {
  799.         aColor = fTextSpecs.theBackColor;
  800.         ChangeBackColor(&aColor);
  801.     }
  802.     fTEView->SetJustification(fTextSpecs.theJustification, kDontRedraw);
  803.     inherited::ShowReverted();
  804. }
  805.  
  806. //--------------------------------------------------------------------------------------------------
  807. #pragma segment AFields
  808.  
  809. pascal void TTextDocument::Fields(pascal void (*DoToField)(StringPtr fieldName, Ptr fieldAddr,
  810.                             short fieldType, void *DoToField_StaticLink), void *DoToField_StaticLink)
  811. {
  812.     (*DoToField)("\pTTextDocument",    NULL, bClass, DoToField_StaticLink);
  813.     (*DoToField)("\pfDocText",            (Ptr) &fDocText, bHandle, DoToField_StaticLink);
  814.     (*DoToField)("\pfTEView",            (Ptr) &fTEView, bObject, DoToField_StaticLink);
  815.     (*DoToField)("\p   fTextSpecs",    NULL, bClass, DoToField_StaticLink);
  816.     (*DoToField)("\p theTextFont",        (Ptr) &fTextSpecs.theTextFont, bString, DoToField_StaticLink);
  817.     (*DoToField)("\p theTextFace",        (Ptr) &fTextSpecs.theTextFace, bStyle, DoToField_StaticLink);
  818.     (*DoToField)("\p theTextSize",        (Ptr) &fTextSpecs.theTextSize, bInteger, DoToField_StaticLink);
  819.     (*DoToField)("\p theTextColor",    (Ptr) &fTextSpecs.theTextColor, bRGBColor, DoToField_StaticLink);
  820.     (*DoToField)("\p theJustification",(Ptr) &fTextSpecs.theJustification, bInteger, DoToField_StaticLink);
  821.     (*DoToField)("\p theBackColor",    (Ptr) &fTextSpecs.theBackColor, bRGBColor, DoToField_StaticLink);
  822.     inherited::Fields(DoToField, DoToField_StaticLink);
  823. }
  824.  
  825. //******************************************************************************************
  826. //  T J u s t C o m m a n d
  827. //******************************************************************************************
  828. //--------------------------------------------------------------------------------------------------
  829. #pragma segment ASelCommand
  830.  
  831. pascal void TJustCommand::IJustCommand(TTEView *itsTEView, short itsNewJust)
  832. {
  833.     ICommand(cJustChange, itsTEView->fDocument, NULL, NULL);
  834.     fTEView = itsTEView;
  835.     fOldJust = itsTEView->fJustification;
  836.     fNewJust = itsNewJust;
  837. }
  838.  
  839. //--------------------------------------------------------------------------------------------------
  840. #pragma segment ADoCommand
  841.  
  842. pascal void TJustCommand::DoIt()
  843. {
  844.     fTEView->SetJustification(fNewJust, kRedraw);
  845. }
  846.  
  847. //--------------------------------------------------------------------------------------------------
  848. #pragma segment ADoCommand
  849.  
  850. pascal void TJustCommand::RedoIt()
  851. {
  852.     DoIt();
  853. }
  854.  
  855. //--------------------------------------------------------------------------------------------------
  856. #pragma segment ADoCommand
  857.  
  858. pascal void TJustCommand::UndoIt()
  859. {
  860.     fTEView->SetJustification(fOldJust, kRedraw);
  861. }
  862.  
  863. //--------------------------------------------------------------------------------------------------
  864. #pragma segment AFields
  865.  
  866. pascal void TJustCommand::Fields(pascal void (*DoToField)(StringPtr fieldName, Ptr fieldAddr,
  867.                           short fieldType, void *DoToField_StaticLink), void *DoToField_StaticLink)
  868. {
  869.     DoToField("\pTJustCommand",    NULL, bClass, DoToField_StaticLink);
  870.     DoToField("\pfTEView",        (Ptr) &fTEView, bObject, DoToField_StaticLink);
  871.     DoToField("\pfOldJust",        (Ptr) &fOldJust, bInteger, DoToField_StaticLink);
  872.     DoToField("\pfNewJust",        (Ptr) &fNewJust, bInteger, DoToField_StaticLink);
  873.     inherited::Fields(DoToField, DoToField_StaticLink);
  874. }
  875.